//
//  XDAddWebPageMgrViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/2.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDAddWebPageMgrViewController.h"



@interface XDAddWebPageMgrViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *webNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *webAddrTextField;

@end

@implementation XDAddWebPageMgrViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = HUDBgColor;
    
}
- (IBAction)sureBtn:(id)sender {
    
    [self.view endEditing:YES];
    
    if (self.webNameTextField.text.length == 0 ) {
        [MBProgressHUD showError:@"请输入网站名称"];
        return;
    }
    
    if(self.webAddrTextField.text.length == 0){
        [MBProgressHUD showError:@"请输入网站地址"];
        return;
    }
    
    NSLog(@"%@--%@",self.webNameTextField.text,self.webAddrTextField.text);
    
    
    NSString *url = BaseURL(@"internet.do?action=saveSetting");
    
    NSDictionary *dict = @{@"addrUrl":self.webAddrTextField.text,
                          @"addrName":self.webNameTextField.text,
                          @"uid":[AccountTool shareAccountTool].currentAccount.uid};
    
    
    [XDHttpTool post:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        NSLog(@"%@",responseObj);
        if ([[[responseObj objectForKey:@"code"] stringValue] isEqualToString:@"1000"]) {
            
            [MBProgressHUD showSuccess:@"添加成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissVC" object:nil];
            [self dismissViewControllerAnimated:YES completion:nil];

        }else{
            [MBProgressHUD showError:@"添加失败"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
    
    
}
- (IBAction)cancelBtn:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

//弹回键盘
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //设置动画的名字
    [UIView beginAnimations:@"Animation" context:nil];
    //设置动画的间隔时间
    [UIView setAnimationDuration:0.20];
    //??使用当前正在运行的状态开始下一段动画
    [UIView setAnimationBeginsFromCurrentState: YES];
    //设置视图移动的位移
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 100, self.view.frame.size.width, self.view.frame.size.height);
    //设置动画结束
    [UIView commitAnimations];
}

//在UITextField 编辑完成调用方法
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    //设置动画的名字
    [UIView beginAnimations:@"Animation" context:nil];
    //设置动画的间隔时间
    [UIView setAnimationDuration:0.20];
    //??使用当前正在运行的状态开始下一段动画
    [UIView setAnimationBeginsFromCurrentState: YES];
    //设置视图移动的位移
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 100, self.view.frame.size.width, self.view.frame.size.height);
    //设置动画结束
    [UIView commitAnimations];
}


@end
